package com.yupi.springbootinit.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.datasource.*;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
import com.yupi.springbootinit.model.dto.search.SearchRequest;
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.model.vo.SearchVo;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.PictureService;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname SearchFacade
 * @Description TODO 搜索门面
 * @Date 2023/11/5 14:44
 * @Created by Daivd
 */
@Component
public class SearchFacade {

    @Resource
    private PostDataSource postDataSource;

    @Resource
    private UserDataSource userDataSource;

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private DataSourceRegistry dataSourceRegistry;

    public SearchVo searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request) {
        /**
         * @Description: 同步写法
         * @Param: [searchRequest, request]
         * @return: com.yupi.springbootinit.common.BaseResponse<com.yupi.springbootinit.model.vo.SearchVo>
         * @Author: Daivd
         * @Date: 2023/10/28
         */
        String type = searchRequest.getType();
        String searchText = searchRequest.getSearchText();
        long pageNum = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();
        if (StringUtils.isBlank(type)) {
            Page<Picture> picturePage = pictureDataSource.doSearch(searchText, pageNum, pageSize);
            UserQueryRequest userQueryRequest = new UserQueryRequest();
            userQueryRequest.setUserName(searchRequest.getSearchText());
            userQueryRequest.setPageSize(pageSize);
            userQueryRequest.setCurrent(pageNum);
            Page<UserVO> userVOPage = userDataSource.doSearch(searchText, pageNum, pageSize);
            PostQueryRequest postQueryRequest = new PostQueryRequest();
            postQueryRequest.setSearchText(searchText);
            postQueryRequest.setPageSize(pageSize);
            postQueryRequest.setCurrent(pageNum);
            Page<PostVO> postVOPage = postDataSource.doSearch(searchText, pageNum, pageSize);
            SearchVo searchVo = new SearchVo();
            searchVo.setUserList(userVOPage.getRecords());
            searchVo.setPostList(postVOPage.getRecords());
            searchVo.setPictureList(picturePage.getRecords());
            return searchVo;
        } else {
            SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
            ThrowUtils.throwIf(searchTypeEnum == null, ErrorCode.PARAMS_ERROR);
            SearchVo searchVo = new SearchVo();
            DataSource dataSource = dataSourceRegistry.getDataSourceByType(type);
            Page page = dataSource.doSearch(searchText, pageNum, pageSize);
            searchVo.setDataList(page.getRecords());
            return searchVo;
        }
    }
}
